<template>
  <div>
    <el-dialog title="分享" :visible.sync="formInline.show">
      <el-form :model="form">
        <el-form-item label="链接" :label-width="formLabelWidth">
          <el-input
            disabled
            style="width:70%"
            v-model="formInline.url"
            autocomplete="off"
          ></el-input>
          <el-button
            style="height:40px;margin-left:10px"
            type="success"
            @click="copy(formInline.url)"
            v-copy="formInline.url"
          >
            一键复制
          </el-button>
        </el-form-item>
        <el-form-item label="二维码" :label-width="formLabelWidth">
          <img :src="getUrl" alt="推广二维码" />
          <el-button type="success" style="margin-left:50px" @click="down">
            <a :href="getUrl" download="二维码" style="color:white">下载图片</a>
          </el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formInline.show = false">取 消</el-button>
        <el-button type="success" @click="formInline.show = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import QRCode from 'qrcode'
import { copy } from 'v-copy'
export default {
  directives: {
    copy
  },
  //   props: ['oneCopyData'],
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          show: false,
          activeId: '',
          url: ''
        }
      }
    }
  },
  data() {
    return {
      url: '',
      getUrl: '',
      dialogFormVisible: true,
      form: {
        name: '1111',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '120px'
    }
  },
  crated() {
    //  this.initUrl()
  },
  mounted() {
    // this.getQRcode(this.url).then((img) => {
    //     console.log('img====>',img)
    //   this.getUrl = img
    // })
  },
  watch: {
    // 'formInline.url': {
    //   handler(newVal, oldVal) {
    //     if (newVal !== '') {
    //       this.getQRcode(this.url).then((img) => {
    //         this.getUrl = img
    //       })
    //     }
    //   },
    //   immediate: true
    // }
  },
  methods: {
    down() {
      let codeUrl = document.createElement('img')
      ;(codeUrl.download = '二维码图片'), (codeUrl.href = this.getUrl)
    },
    async getQRcode(url) {
      console.log('url===<', url)
      const cofing = {
        errorCorrectionLevel: 'L',
        type: 'image/jpeg',
        quality: 1,
        margin: 1,
        color: {
          dark: '#000',
          light: '#fff'
        }
      }
      try {
        const qrcode = await QRCode.toDataURL(url, cofing)
        return qrcode
      } catch (err) {}
    },
    // initUrl() {
    //   let _this = this
    //   let obj = {
    //     configKey: 'marketing_activity_url',
    //   }
    //   getCommUrl(
    //     obj,
    //     (res) => {
    //       this.url =res + '/'+this.formInline.activeId
    //     },
    //     (err) => {}
    //   )
    // },
    copy(val) {
      if (val) {
        this.$message.success('已复制！')
      } else {
        this.$message.warning('无数据！')
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
